﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ArcEngine.Controllers;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using stdole;
using System.IO;
using ESRI.ArcGIS.DataSourcesRaster;
using DIWAGIS.Modules;
namespace ArcEngine.Views
{
    public partial class frm_pagePrintSetup : Form
    {
        public frm_pagePrintSetup()
        {
            InitializeComponent();
        }

        public delegate void activeMapControl();

        public activeMapControl _activeMap;

        ////////

        IMapFrame pMapFrame;
        IGraphicsContainer pGraphicsContainer;
        IMap pMap;
        IActiveView pActiveView;

        ////////

        private void frm_pagePrintSetup_Load(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add("01", "Khâu răng ngoài D27 UPVC", "1 cái");
            dataGridView1.Rows.Add("02", "Đầu nối HDPE D25", "1 cái");
            dataGridView1.Rows.Add("03", "Ống HDPE 25mm", "1m");
            dataGridView1.Rows.Add("04", "Van cổng D150 có van 1 chiều", "1 cái");
            dataGridView1.Rows.Add("05", "TLK 25mm", "1 cái");
            dataGridView1.Rows.Add("06", "Van cóc sau", "1 cái");
            dataGridView1.Rows.Add("07", "Khâu răng trong PVC D27", "1 cái");
            dataGridView1.Rows.Add("08", "Coude PVC D27", "1 cái");
            dataGridView1.Rows.Add("09");
            dataGridView1.Rows.Add("10");
            dataGridView1.Rows.Add("11");
            dataGridView1.Rows.Add("12");

            //////////////////////////
            
            generalVariable.pageLayoutControl.Page.FormID = esriPageFormID.esriPageFormA4;
            generalVariable.pageLayoutControl.Page.Units = esriUnits.esriCentimeters;

            pActiveView = generalVariable.pageLayoutControl.ActiveView;
            pMap = generalVariable.pageLayoutControl.ActiveView.FocusMap;

            pGraphicsContainer = generalVariable.pageLayoutControl.GraphicsContainer;

            pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame;
                     
        }


        private void cmdPrint_Click(object sender, EventArgs e)
        {
            if (generalVariable.pageLayoutControl.Printer != null)
            {
                //Set mouse pointer
                generalVariable.pageLayoutControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;

                //Get IPrinter interface through the PageLayoutControl's printer
                IPrinter printer = generalVariable.pageLayoutControl.Printer;

                //Determine whether printer paper's orientation needs changing
                if (printer.Paper.Orientation != generalVariable.pageLayoutControl.Page.Orientation)
                {
                    printer.Paper.Orientation = generalVariable.pageLayoutControl.Page.Orientation;
                }

                //Print the page range with the specified overlap
                generalVariable.pageLayoutControl.PrintPageLayout(1, 1, 0);

                //Set the mouse pointer
                generalVariable.pageLayoutControl.MousePointer = esriControlsMousePointer.esriPointerDefault;

                if (_activeMap != null)
                {
                    _activeMap();
                }
                this.Close();

            }
        }

        private void bt_Cancel_Click(object sender, EventArgs e)
        {
            if (_activeMap != null)
            {
                _activeMap();
            }
            this.Close();
        }

        private void bt_openFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog f;
            f = new OpenFileDialog();
            f.Filter = "Image File | *.png | File | *.jpg";
            f.ShowDialog();

            if (f.FileName.Length > 0)
            {
                tenHS.Text = f.FileName;
            }
        }
        
        private void addText(string text, IEnvelope pEnv)
        {
            //Create a text element
            ITextElement textElement;
            ITextSymbol m_textSymbol = new TextSymbol();

            m_textSymbol.Angle = 0;
            m_textSymbol.RightToLeft = false;
            m_textSymbol.VerticalAlignment = esriTextVerticalAlignment.esriTVABaseline;
            m_textSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHAFull;

            textElement = new TextElementClass();
            textElement.Text = text;

            pEnv.PutCoords(8.85, 3.85, 14.67, 3.46);

            //Set the TextElement symbol to that of the selected text symbol
            textElement.Symbol = m_textSymbol;
            textElement.ScaleText = true;

            //QI to IElment
            IElement element = (IElement)textElement;
            //Set the TextElement's geometry
            element.Geometry = pEnv;

            //Add the element to the GraphicsContainer
            generalVariable.pageLayoutControl.ActiveView.GraphicsContainer.AddElement(element, 0);
            //Refresh the PageLayout
            generalVariable.pageLayoutControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, element, null);
        }

        private void bt_setup_Click(object sender, EventArgs e)
        {
            ////////////////////////
            addElement();
            cmdPrint.Enabled = true;

        }


        //////////////////////////

        private IElement GetPicture(string sPath)
        {
            if (File.Exists(sPath) == false)
            {
                //frm_message.showMesage("Không tìm thấy file có đường dẫn: " + sPath, 1);
                return null;
            }
            IRasterPicture pRasterPicture;
            pRasterPicture = new RasterPictureClass();
            IOlePictureElement pOlePicture;
            pOlePicture = new BmpPictureElementClass();
            pOlePicture.ImportPicture(pRasterPicture.LoadPicture(sPath) as IPictureDisp);
            return pOlePicture as IElement;
        }


        private void setTile(string nameTile, IEnvelope pEnv, byte snum, Boolean bold = false, esriTextVerticalAlignment VAlign = esriTextVerticalAlignment.esriTVACenter, esriTextHorizontalAlignment HAlign = esriTextHorizontalAlignment.esriTHACenter)
        {
            IActiveView pActiveView;
            IGraphicsContainer pGraphicsContainer;
            ITextElement pTextElement;
            IElement pElement;
            IFontDisp myFont;

            myFont = new StdFont() as IFontDisp;
            //'Check ArcMap is in layout view

            //If Not pMxDoc.ActiveView Is pMxDoc.PageLayout Then Exit Sub
            pActiveView = generalVariable.pageLayoutControl.ActiveView;
            pGraphicsContainer = generalVariable.pageLayoutControl.GraphicsContainer;

            pTextElement = new TextElement() as ITextElement;
            pElement = pTextElement as IElement;

            ITextSymbol pSymbol;
            pSymbol = new TextSymbol();
            myFont.Size = snum;
            myFont.Name = "Times New Roman";
            myFont.Bold = bold;
            pSymbol.Font = myFont;

            pSymbol.VerticalAlignment = VAlign;
            pSymbol.HorizontalAlignment = HAlign;
            pTextElement.Text = nameTile;

            pTextElement.Symbol = pSymbol;
            pElement.Geometry = pEnv;

            pGraphicsContainer.AddElement(pTextElement as IElement, 0);
            pActiveView = null;
            pGraphicsContainer = null;
            pTextElement = null;
            pElement = null;
        }


        ///////////

        /////////////////////////

        private void addElement()
        {
            IEnvelope pEnv;
            IElement pElement;
            string sTA = "CTY TNHH XÂY DỰNG VÀ CẤP NƯỚC TT-HUẾ";
            IPage pPage;
            this.Cursor = Cursors.WaitCursor;
            pElement = pMapFrame as IElement;
            pEnv = new Envelope() as IEnvelope;
            // 'Tran in dung
            pPage = generalVariable.pageLayoutControl.Page;
            pPage.Orientation = 1;
            pPage.FormID = esriPageFormID.esriPageFormA4;
            pEnv.PutCoords(1.21, 24.47, 19.84, 14.35);
            pElement.Geometry = pEnv;
            pGraphicsContainer.AddElement(pElement, 0);

            pPage.StretchGraphicsWithPage = true;

            AddNeatlineFrame(0.55, 3.22, 8.03, 0.6);
            pEnv.PutCoords(2.62, 3, 4.89, 2.62);
            setTile("Duyệt của BGĐ", pEnv, 10, true);

            AddNeatlineFrame(0.55, 4.02, 8.03, 3.22);
            pEnv.PutCoords(0.55, 4.02, 8.03, 3.22);
            setTile(sTA, pEnv, 10, true);

            AddNeatlineFrame(8.03, 4.02, 15.9, 0.6);
            pEnv.PutCoords(8.85, 3.85, 14.67, 3.46);
            setTile("NÂNG, DỜI, THAY HỆ THỐNG", pEnv, 10, true);
            pEnv.PutCoords(1.1, 3.15, 15.9, 2.75);

            //Dim dt As DataTable = GetDataTableKH("select top 1 danhbo1, HoTen, SoNha, Duong.TenDuong, DienThoai from KhachHang,Duong where idkh='" + pFeature.Value(pFeature.Fields.FindField("MaKhachHang")) + "' and KhachHang.msd=Duong.idduong");
            //If dt.Rows.Count > 0 Then
            //    setTile("Tên: " + RTrim(dt.Rows(0).Item("HoTen").ToString), pEnv, 10, False, esriTextVerticalAlignment.esriTVACenter, esriTextHorizontalAlignment.esriTHALeft)
            //    pEnv.PutCoords(1.1, 2.53, 15.9, 2.13)
            //    setTile("Danh bộ: " + RTrim(dt.Rows(0).Item("danhbo1").ToString), pEnv, 10, False, esriTextVerticalAlignment.esriTVACenter, esriTextHorizontalAlignment.esriTHALeft)
            //    pEnv.PutCoords(1.1, 1.93, 15.9, 1.53)
            //    setTile("Địa chỉ: " + RTrim(dt.Rows(0).Item("SoNha").ToString) + " - " + RTrim(dt.Rows(0).Item("TenDuong").ToString), pEnv, 10, False, esriTextVerticalAlignment.esriTVACenter, esriTextHorizontalAlignment.esriTHALeft)
            //    pEnv.PutCoords(1.1, 1.33, 15.9, 0.93)
            //    setTile("Điện thoại: " + RTrim(dt.Rows(0).Item("DienThoai").ToString), pEnv, 10, False, esriTextVerticalAlignment.esriTVACenter, esriTextHorizontalAlignment.esriTHALeft)
            //Else
            //    setTile("Tên: ", pEnv, 10, False, esriTextVerticalAlignment.esriTVACenter, esriTextHorizontalAlignment.esriTHALeft)
            //    pEnv.PutCoords(1.1, 2.53, 15.9, 2.13)
            //    setTile("Danh bộ: ", pEnv, 10, False, esriTextVerticalAlignment.esriTVACenter, esriTextHorizontalAlignment.esriTHALeft)
            //    pEnv.PutCoords(1.1, 1.93, 15.9, 1.53)
            //    setTile("Địa chỉ: ", pEnv, 10, False, esriTextVerticalAlignment.esriTVACenter, esriTextHorizontalAlignment.esriTHALeft)
            //    pEnv.PutCoords(1.1, 1.33, 15.9, 0.93)
            //    setTile("Điện thoại: ", pEnv, 10, False, esriTextVerticalAlignment.esriTVACenter, esriTextHorizontalAlignment.esriTHALeft)
            //End If

            AddNeatlineFrame(15.9, 4.02, 20.45, 1.53);
            pEnv.PutCoords(15.9, 4.02, 20.45, 3.48);
            setTile("Khảo sát thiết kế: ", pEnv, 10, true);

            if (check_khaosat.Checked)
            {
                pEnv.PutCoords(15.9, 2.19, 20.45, 1.53);
                setTile(khaosatthietke.Text, pEnv, 10, true);
            }

            AddNeatlineFrame(15.9, 1.53, 20.45, 0.6);
            if (check_Date.Checked)
            {
                pEnv.PutCoords(15.9, 1.53, 20.45, 0.6);
                setTile(ngay.Text, pEnv, 10, true);
            }

            pEnv.PutCoords(0.05, 27.5, 4.6, 27.48);
            setTile("CTY TNHH MTV XD VÀ CN \n THỪA THIÊN HUẾ", pEnv, 8, false);

            // logo
            IPictureElement pPicElem1;
            pEnv.PutCoords(1.2, 27, 3, 25);
            pElement = GetPicture(AppDomain.CurrentDomain.BaseDirectory + @"\system\logohuewaco.png");

            if (pElement != null)
            {
                pPicElem1 = pElement as IPictureElement;
                pPicElem1.MaintainAspectRatio = false;
                pPicElem1.SavePictureInDocument = true;
                pElement.Geometry = pEnv;
                pGraphicsContainer.AddElement(pElement, 0);
            }
            pEnv.PutCoords(8.06, 27.5, 16.86, 26.99);
            setTile("PHIẾU BÁO CÁO THI CÔNG \n LẮP MỚI,NÂNG, DỜI TLK", pEnv, 16, true);

            AddNeatlineFrame(0.55, 5.32, 1.15, 4.67);
            pEnv.PutCoords(0.55, 5.32, 1.15, 4.67);
            setTile("04", pEnv, 10, true);
            AddNeatlineFrame(1.15, 5.32, 6.06, 4.67);
            pEnv.PutCoords(1.15, 5.32, 6.06, 4.67);

            if (dataGridView1.Rows[3].Cells[1].Value != null)
            {
                setTile(dataGridView1.Rows[3].Cells[1].Value.ToString(), pEnv, 10);
            }
            AddNeatlineFrame(6.06, 5.32, 7.11, 4.67);
            pEnv.PutCoords(6.06, 5.32, 7.11, 4.67);
            if (dataGridView1.Rows[3].Cells[2].Value != null)
            {
                setTile(dataGridView1.Rows[3].Cells[2].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(7.11, 5.32, 7.75, 4.67);
            pEnv.PutCoords(7.11, 5.32, 7.75, 4.67);
            setTile("08", pEnv, 10, true);
            AddNeatlineFrame(7.75, 5.32, 12.88, 4.67);
            pEnv.PutCoords(7.75, 5.32, 12.88, 4.67);

            if (dataGridView1.Rows[7].Cells[1].Value != null)
            {
                setTile(dataGridView1.Rows[7].Cells[1].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(12.88, 5.32, 13.87, 4.67);
            pEnv.PutCoords(12.88, 5.32, 13.87, 4.67);
            if (dataGridView1.Rows[7].Cells[2].Value != null)
            {
                setTile(dataGridView1.Rows[7].Cells[2].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(13.87, 5.32, 14.48, 4.67);
            pEnv.PutCoords(13.87, 5.32, 14.48, 4.67);
            setTile("12", pEnv, 10, true);
            AddNeatlineFrame(14.48, 5.32, 19.45, 4.67);
            pEnv.PutCoords(14.48, 5.32, 19.45, 4.67);
            if (dataGridView1.Rows[11].Cells[1].Value != null)
            {
                setTile(dataGridView1.Rows[11].Cells[1].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(19.45, 5.32, 20.45, 4.67);
            pEnv.PutCoords(19.45, 5.32, 20.45, 4.67);
            if (dataGridView1.Rows[11].Cells[2].Value != null)
            {
                setTile(dataGridView1.Rows[11].Cells[2].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(0.55, 5.97, 1.15, 5.32);
            pEnv.PutCoords(0.55, 5.97, 1.15, 5.32);
            setTile("03", pEnv, 10, true);
            AddNeatlineFrame(1.15, 5.97, 6.06, 5.32);
            pEnv.PutCoords(1.15, 5.97, 6.06, 5.32);
            if (dataGridView1.Rows[2].Cells[1].Value != null)
            {
                setTile(dataGridView1.Rows[2].Cells[1].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(6.06, 5.97, 7.11, 5.32);
            pEnv.PutCoords(6.06, 5.97, 7.11, 5.32);
            if (dataGridView1.Rows[2].Cells[2].Value != null)
            {
                setTile(dataGridView1.Rows[2].Cells[2].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(7.11, 5.97, 7.75, 5.32);
            pEnv.PutCoords(7.11, 5.97, 7.75, 5.32);
            setTile("07", pEnv, 10, true);
            AddNeatlineFrame(7.75, 5.97, 12.88, 5.32);
            pEnv.PutCoords(7.75, 5.97, 12.88, 5.32);
            if (dataGridView1.Rows[6].Cells[1].Value != null)
            {
                setTile(dataGridView1.Rows[6].Cells[1].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(12.88, 5.97, 13.87, 5.32);
            pEnv.PutCoords(12.88, 5.97, 13.87, 5.32);
            if (dataGridView1.Rows[6].Cells[2].Value != null)
            {
                setTile(dataGridView1.Rows[6].Cells[2].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(13.87, 5.97, 14.48, 5.32);
            pEnv.PutCoords(13.87, 5.97, 14.48, 5.32);
            setTile("11", pEnv, 10, true);
            AddNeatlineFrame(14.48, 5.97, 19.45, 5.32);
            pEnv.PutCoords(14.48, 5.97, 19.45, 5.32);
            if (dataGridView1.Rows[10].Cells[1].Value != null)
            {
                setTile(dataGridView1.Rows[10].Cells[1].Value.ToString(), pEnv, 10);
            }
            AddNeatlineFrame(19.45, 5.97, 20.45, 5.32);
            pEnv.PutCoords(19.45, 5.97, 20.45, 5.32);
            if (dataGridView1.Rows[10].Cells[2].Value != null)
            {
                setTile(dataGridView1.Rows[10].Cells[2].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(0.55, 6.62, 1.15, 5.97);
            pEnv.PutCoords(0.55, 6.62, 1.15, 5.97);
            setTile("02", pEnv, 10, true);
            AddNeatlineFrame(1.15, 6.62, 6.06, 5.97);
            pEnv.PutCoords(1.15, 6.62, 6.06, 5.97);
            if (dataGridView1.Rows[1].Cells[1].Value != null)
            {
                setTile(dataGridView1.Rows[1].Cells[1].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(6.06, 6.62, 7.11, 5.97);
            pEnv.PutCoords(6.06, 6.62, 7.11, 5.97);
            if (dataGridView1.Rows[1].Cells[2].Value != null)
            {
                setTile(dataGridView1.Rows[1].Cells[2].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(7.11, 6.62, 7.75, 5.97);
            pEnv.PutCoords(7.11, 6.62, 7.75, 5.97);
            setTile("06", pEnv, 10, true);
            AddNeatlineFrame(7.75, 6.62, 12.88, 5.97);
            pEnv.PutCoords(7.75, 6.62, 12.88, 5.97);
            if (dataGridView1.Rows[5].Cells[1].Value != null)
            {
                setTile(dataGridView1.Rows[5].Cells[1].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(12.88, 6.62, 13.87, 5.97);
            pEnv.PutCoords(12.88, 6.62, 13.87, 5.97);
            if (dataGridView1.Rows[5].Cells[2].Value != null)
            {
                setTile(dataGridView1.Rows[5].Cells[2].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(13.87, 6.62, 14.48, 5.97);
            pEnv.PutCoords(13.87, 6.62, 14.48, 5.97);
            setTile("10", pEnv, 10, true);
            AddNeatlineFrame(14.48, 6.62, 19.45, 5.97);
            pEnv.PutCoords(14.48, 6.62, 19.45, 5.97);
            if (dataGridView1.Rows[9].Cells[1].Value != null)
            {
                setTile(dataGridView1.Rows[9].Cells[1].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(19.45, 6.62, 20.45, 5.97);
            pEnv.PutCoords(19.45, 6.62, 20.45, 5.97);
            if (dataGridView1.Rows[9].Cells[2].Value != null)
            {
                setTile(dataGridView1.Rows[9].Cells[2].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(0.55, 7.27, 1.15, 6.62);
            pEnv.PutCoords(0.55, 7.27, 1.15, 6.62);
            setTile("01", pEnv, 10, true);
            AddNeatlineFrame(1.15, 7.27, 6.06, 6.62);
            pEnv.PutCoords(1.15, 7.27, 6.06, 6.62);
            if (dataGridView1.Rows[0].Cells[1].Value != null)
            {
                setTile(dataGridView1.Rows[0].Cells[1].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(6.06, 7.27, 7.11, 6.62);
            pEnv.PutCoords(6.06, 7.27, 7.11, 6.62);
            if (dataGridView1.Rows[0].Cells[2].Value != null)
            {
                setTile(dataGridView1.Rows[0].Cells[2].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(7.11, 7.27, 7.75, 6.62);
            pEnv.PutCoords(7.11, 7.27, 7.75, 6.62);
            setTile("05", pEnv, 10, true);
            AddNeatlineFrame(7.75, 7.27, 12.88, 6.62);
            pEnv.PutCoords(7.75, 7.27, 12.88, 6.62);
            if (dataGridView1.Rows[4].Cells[1].Value != null)
            {
                setTile(dataGridView1.Rows[4].Cells[1].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(12.88, 7.27, 13.87, 6.62);
            pEnv.PutCoords(12.88, 7.27, 13.87, 6.62);
            if (dataGridView1.Rows[4].Cells[2].Value != null)
            {
                setTile(dataGridView1.Rows[4].Cells[2].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(13.87, 7.27, 14.48, 6.62);
            pEnv.PutCoords(13.87, 7.27, 14.48, 6.62);
            setTile("09", pEnv, 10, true);
            AddNeatlineFrame(14.48, 7.27, 19.45, 6.62);
            pEnv.PutCoords(14.48, 7.27, 19.45, 6.62);
            if (dataGridView1.Rows[8].Cells[1].Value != null)
            {
                setTile(dataGridView1.Rows[8].Cells[1].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(19.45, 7.27, 20.45, 6.62);
            pEnv.PutCoords(19.45, 7.27, 20.45, 6.62);
            if (dataGridView1.Rows[8].Cells[2].Value != null)
            {
                setTile(dataGridView1.Rows[8].Cells[2].Value.ToString(), pEnv, 10);
            }

            AddNeatlineFrame(0.55, 7.93, 1.15, 7.27);
            pEnv.PutCoords(0.55, 7.93, 1.15, 7.27);
            setTile("STT", pEnv, 8, true);
            AddNeatlineFrame(1.15, 7.93, 6.06, 7.27);
            pEnv.PutCoords(1.15, 7.93, 6.06, 7.27);
            setTile("VẬT TƯ", pEnv, 10, true);
            AddNeatlineFrame(6.06, 7.93, 7.11, 7.27);
            pEnv.PutCoords(6.06, 7.93, 7.11, 7.27);
            setTile("SL", pEnv, 8, true);
            AddNeatlineFrame(7.11, 7.93, 7.75, 7.27);
            pEnv.PutCoords(7.11, 7.93, 7.75, 7.27);
            setTile("STT", pEnv, 8, true);
            AddNeatlineFrame(7.75, 7.93, 12.88, 7.27);
            pEnv.PutCoords(7.75, 7.93, 12.88, 7.27);
            setTile("VẬT TƯ", pEnv, 10, true);
            AddNeatlineFrame(12.88, 7.93, 13.87, 7.27);
            pEnv.PutCoords(12.88, 7.93, 13.87, 7.27);
            setTile("SL", pEnv, 8, true);
            AddNeatlineFrame(13.87, 7.93, 14.48, 7.27);
            pEnv.PutCoords(13.87, 7.93, 14.48, 7.27);
            setTile("STT", pEnv, 8, true);
            AddNeatlineFrame(14.48, 7.93, 19.45, 7.27);
            pEnv.PutCoords(14.48, 7.93, 19.45, 7.27);
            setTile("VẬT TƯ", pEnv, 10, true);
            AddNeatlineFrame(19.45, 7.93, 20.45, 7.27);
            pEnv.PutCoords(19.45, 7.93, 20.45, 7.27);
            setTile("SL", pEnv, 8, true);

            AddNeatlineFrame(0.55, 4.67, 20.45, 4.02);
            pEnv.PutCoords(0.55, 4.67, 20.45, 4.02);
            setTile("Ghi chú nếu thay đổi...................................................................................................................................................", pEnv, 10, false);

            pEnv.PutCoords(11.35, 13.73, 17, 13.23);
            setTile("SƠ ĐỒ LẮP ĐẶT THỦY LỰC KẾ", pEnv, 12, true);

            IPictureElement pPicElem;
            pEnv.PutCoords(8.17, 13.07, 19.65, 8.04);
            if (radio_NangDoiHT.Checked)
            {
                pElement = GetPicture(AppDomain.CurrentDomain.BaseDirectory + @"\system\Mau.jpg");
            }

            if (radio_nangdoicumTLK.Checked)
            {
                pElement = GetPicture(AppDomain.CurrentDomain.BaseDirectory + @"\system\NangDoiCumTLK.jpg");
            }
            if (radio_HDPE.Checked)
            {
                pElement = GetPicture(AppDomain.CurrentDomain.BaseDirectory + @"\system\NangDaCoOngHDPE.jpg");
            }

            if (tenHS.Text.Trim().Length > 0)
            {
                pElement = GetPicture(tenHS.Text.Trim());
            }

            if (pElement != null)
            {
                pPicElem = pElement as IPictureElement;
                pPicElem.MaintainAspectRatio = false;
                pPicElem.SavePictureInDocument = true;
                pElement.Geometry = pEnv;
                pGraphicsContainer.AddElement(pElement, 0);
            }

            if (check_ghichu.Checked)
            {
                pEnv.PutCoords(0.05, 13.93, 2.18, 13.61);
                setTile("* Ghi chú :", pEnv, 10);

                pEnv.PutCoords(-17.65, 14.28, 20.45, 12.49);
                setTile(ghichu.Text, pEnv, 10, false, esriTextVerticalAlignment.esriTVATop, esriTextHorizontalAlignment.esriTHALeft);
            }

            pEnv.PutCoords(1.06, 10.43, 6.61, 10.06);
            setTile("VỊ TRÍ LẮP ĐẶT THỦY LỰC KẾ", pEnv, 10);
            pEnv.PutCoords(1.06, 9.86, 6.61, 9.55);
            setTile("( THEO VỊ TRÍ KHU ĐẤT, NHÀ )", pEnv, 8);
            if (check_vitri.Checked)
            {
                pEnv.PutCoords(1.06, 9.33, 6.61, 8.93);
                setTile(vitrilapdat.Text, pEnv, 10);
            }
         
            //'If chkChonMuiTen.Checked Then
            //'    pEnv.PutCoords(0.4, 28.5, 0.7, 27.9)
            //'    'AddNeatline(27.9)
            //'    pID.Value = "esriCarto.MarkerNorthArrow"

            //'    pMapSurround = CreateSurround(pID, pEnv, "North Arrow", pMxDoc.FocusMap, pMxDoc.PageLayout)

            //'    'Change out the default north arrow
            //'    pMarkerNorthArrow = pMapSurround 'QI

            //'    pCharacterMarkerSymbol = pMarkerNorthArrow.MarkerSymbol 'clones the symbol
            //'    pCharacterMarkerSymbol.CharacterIndex = 177 'change the symbol
            //'    pCharacterMarkerSymbol.Size = 60
            //'    pMarkerNorthArrow.MarkerSymbol = pCharacterMarkerSymbol 'set it back

            //'End If
            pActiveView.Refresh();
            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
        }


        ////////////////////////////

        private void AddNeatlineFrame(double xMin, double yMin, double xMax, double yMax)
        {
            IScreenDisplay pDisplay;
            IElement pElem;
            IMapFrame pMapFrame;
            IEnvelope pEnv;
            IEnvelope pElemEnvelope = null;
            string strMapName;
            IGraphicsContainer pGC;

            pGC = generalVariable.pageLayoutControl.GraphicsContainer;
            pDisplay = generalVariable.pageLayoutControl.ActiveView.ScreenDisplay;

            pMap = generalVariable.pageLayoutControl.ActiveView.FocusMap;
            strMapName = pMap.Name;

            //'Get the active map's Map Frame out of the graphics container
            pGC.Reset();
            pElem = pGC.Next();
            do
            {
                if (pElem is IFrameElement)
                {
                    if (pElem is IMapFrame)
                    {
                        pMapFrame = pElem as IMapFrame;
                        if (strMapName.ToUpper() == pMapFrame.Map.Name.ToUpper())
                        {
                            pElemEnvelope = pElem.Geometry.Envelope;
                            pEnv = pMapFrame.MapBounds;
                        }
                    }
                }
                pElem = pGC.Next();

            }
            while (pElem != null);

            RectangleElement pRect;
            pRect = new RectangleElement();

            //'Set the Neatline distance from the frame border
            pElemEnvelope = new Envelope() as IEnvelope;
            pElemEnvelope.XMin = xMin;
            pElemEnvelope.YMin = yMin;
            pElemEnvelope.XMax = xMax;
            pElemEnvelope.YMax = yMax;

            //'Set the Neatline symbology
            IFillShapeElement pRectSym;
            pRectSym = pRect as IFillShapeElement;

            //'Set the style of the fill symbol
            ISimpleFillSymbol pFillSymbol;
            ISimpleLineSymbol pLineSymbol;

            pFillSymbol = new SimpleFillSymbol();
            pLineSymbol = pFillSymbol.Outline as SimpleLineSymbol;

            pFillSymbol.Style = esriSimpleFillStyle.esriSFSHollow;


            //'Set the outline symbol
            pLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
            pLineSymbol.Width = 1;
            pFillSymbol.Outline = pLineSymbol;

            //'Set it all into the element
            pRectSym.Symbol = pFillSymbol;
            //'Draw the neat line
            generalVariable.pageLayoutControl.AddElement(pRect, pElemEnvelope, pFillSymbol,null,-1);

        }

        private void radio_NangDoiHT_CheckedChanged(object sender, EventArgs e)
        {

        }
        //////////////////////////////////////

    }
}
